package net.lainiao.demo;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import net.lainiao.MyConst;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created by Administrator on 2018/7/9.
 */
public class CustomDemo {
    public static void main(String[] args) throws IOException, TimeoutException {
        for (int i = 0; i <50 ; i++) {
            final  int messNum=i;
            final Channel channel= MyConst.getChanel();
            channel.basicQos(1);
            channel.queueDeclare(MyConst.QName,false,false,false,null);
            channel.basicConsume(MyConst.QName,false,new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println(messNum+"接受到消息:"+new String(body));
                    try {
                        Thread.sleep(messNum*10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    channel.basicAck(envelope.getDeliveryTag(),false);
                }
            });
        }
    }
}
